package com.sdsy.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sdsy.dao.CommDao;
import com.sdsy.dao.TransferAccountsExcelDao;
import com.sdsy.domain.TransferAccountsExcelDO;
import com.sdsy.service.TblTransferAccountService;
import com.sdsy.service.TransferAccountsExcelService;
import com.sdsy.util.ImportExcelUtil;
import com.sdsy.util.SysUtil;
import com.sdsy.vo.TransferAccountsExcelVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * (TransferAccountsRecord)表服务实现类
 *
 * @author yxc
 * @since 2018-09-03 11:52:02
 */
@Service
public class TransferAccountsExcelServiceImpl extends ServiceImpl<TransferAccountsExcelDao, TransferAccountsExcelDO> implements TransferAccountsExcelService {

    private Logger logger = LoggerFactory.getLogger(TransferAccountsExcelServiceImpl.class);
    @Autowired
    private TransferAccountsExcelDao transferAccountsExcelDao;
    @Autowired
    private TblTransferAccountService tblTransferAccountService;
    @Autowired
    private CommDao commDao;

    public static void main(String[] args) {
        String bb = "0.10000000";
        boolean flag = isNumber(bb);
        System.out.println(flag);
    }

    @Override
    public List<TransferAccountsExcelDO> excelForData(InputStream in, String path) {
        List<TransferAccountsExcelDO> aa = new ArrayList<TransferAccountsExcelDO>();
        try {
            List<List<Object>> resultList = new ImportExcelUtil().importDataFromExcel(in, path);
            for (int i = 0; i < resultList.size(); i++) {
                TransferAccountsExcelDO excelDO = new TransferAccountsExcelDO();
                List<Object> lo = resultList.get(i);
                if (String.valueOf(lo.get(0)) != null && !"".equals(String.valueOf(lo.get(0)))) {
                    if (String.valueOf(lo.get(0)).length() < 42) {
                        continue;
                    } else {
                        excelDO.setToAccountNo(String.valueOf(lo.get(0)));
                    }
                } else {
                    continue;
                }

                String num = String.valueOf(lo.get(1));
                if (num != null && !num.equals("")) {
                    if (isNumber(num)) {
                        excelDO.setTransferNum(num);
                    } else {
                        continue;
                    }
                } else {
                    continue;
                }

                excelDO.setCreateTime(new Date());
                excelDO.setStatus("0");
                transferAccountsExcelDao.insert(excelDO);
                aa.add(excelDO);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return aa;
    }


    public static boolean isNumber(String str) {
        java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,8})?$"); // 判断小数点后2位的数字的正则表达式
        java.util.regex.Matcher match = pattern.matcher(str);
        if (match.matches() == false) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public List<TransferAccountsExcelDO> queryList() {
        List<TransferAccountsExcelDO> list = transferAccountsExcelDao.selectList(new EntityWrapper<TransferAccountsExcelDO>().eq("status", "0").isNull("hash"));
//        if (!SysUtil.isNull(list) && list.size() > 0) {
//            for (TransferAccountsExcelDO vo : list) {
//                List<TblTransferAccountDO> ll = tblTransferAccountService.selectList(new EntityWrapper<TblTransferAccountDO>().eq("status", "0"));
//                if (!SysUtil.isNull(ll) && ll.size() > 0) {
//                    vo.setFromAccountNo(ll.get(0).getAddress());
//                    String remark = ll.get(0).getRemark();
//                    // 构建
//                    byte[] key = Base64Utils.decodeFromString(remark);
//                    AES aes = SecureUtil.aes(key);
//                    String decryptStr = aes.decryptStr(ll.get(0).getPrivateKey(), CharsetUtil.CHARSET_UTF_8);
//                    vo.setPrivateKey(decryptStr);
//                }
//            }
//        }
        return list;
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public boolean updateTransferAccounts(TransferAccountsExcelDO vo) {
        vo.setUpdateTime(DateUtil.parse(commDao.now()));
        vo.setRemark("更新交易hash值");
        TransferAccountsExcelDO v1 = getTransferAccountsExcelDO(vo);
        Integer count = transferAccountsExcelDao.updateById(v1);
        if (count > 0) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * @param vo
     * @return
     */
    public TransferAccountsExcelDO getTransferAccountsExcelDO(TransferAccountsExcelDO vo) {
        TransferAccountsExcelDO v1 = transferAccountsExcelDao.selectById(vo.getId());
        v1.setHash(vo.getHash());
        v1.setRemark(vo.getRemark());
        v1.setUpdateTime(vo.getUpdateTime());
        v1.setStatus(vo.getStatus());
        v1.setFromAccountNo(vo.getFromAccountNo());
        return v1;
    }

    @Override
    public int count(Map<String, Object> map) {

        return transferAccountsExcelDao.count(map);

    }

    @Override
    public List<TransferAccountsExcelVO> list(Map<String, Object> map) {
        List<TransferAccountsExcelDO> list = transferAccountsExcelDao.list(map);
        List<TransferAccountsExcelVO> resultList = new ArrayList<>();
        for (TransferAccountsExcelDO excelDo : list) {
            TransferAccountsExcelVO vo = new TransferAccountsExcelVO();
            if (excelDo.getStatus().equals("0")) {
                vo.setStatus("未转账");
            } else if (excelDo.getStatus().equals("1")) {
                vo.setStatus("已转账");
            } else if (excelDo.getStatus().equals("2")) {
                vo.setStatus("转账中");
            } else {
                vo.setStatus("转账失败");
            }
            vo.setTransferNum(excelDo.getTransferNum());
            vo.setToAccountNo(excelDo.getToAccountNo());
            vo.setFromAccountNo(excelDo.getFromAccountNo());
            vo.setId(excelDo.getId());
            vo.setCreateTime(excelDo.getCreateTime());
            resultList.add(vo);
        }
        return resultList;
    }
}